




from PySide6 import QtCore
from PySide6.QtCore import Slot, QTimer, Qt, QDate, QTime, Signal, SLOT, QByteArray, QDateTime,QCoreApplication
from PySide6.QtGui import QColor, QImage, QTextCursor
from PySide6.QtWidgets import QWidget, QMessageBox, QLineEdit, QCheckBox, QTableWidgetItem, QComboBox, QInputDialog, \
    QFileDialog, QDateTimeEdit,QDialog

from gui.ui.UI_main_window import Ui_MainWindow
from gui.ui.UI_login_synchronous import Ui_Form_login_synchronous

import requests

class Login_Synchronous_Panel(Ui_Form_login_synchronous, QDialog):
    def __init__(self, parent=None):
        super(Login_Synchronous_Panel, self).__init__()
        """
        1. 检查登录状态
        2. pushButton_synchronous 未登录不可点击
        2. pushButton_synchronous 定时检查更新登录状态
        """
        self.setupUi(self)
        self.parentWidget: QWidget = parent
        self.qtimer_check_login_state = QTimer()
        self.qtimer_check_login_state.timeout.connect(self.check_login_state)
        self.pushButton_user_login.clicked.connect(self.login_request)
        
        
    def show(self):
        username = self.parentWidget.settings.value("username")
        password = self.parentWidget.settings.value("password")
        if username:
            self.lineEdit_username.setText(username)
        if password:
            self.lineEdit_password.setText(password)
        super().show()
    def login_request(self):
        username = self.lineEdit_username.text()
        password = self.lineEdit_password.text()
        print(f"登录请求: {username}")
        self.parentWidget.api_client.login(username,password) # 登录
        # if response.status_code == 200:
        #     body_dict = response.json()
        #     print(f"登录请求返回 {body_dict.get('code')}: {body_dict.get('msg')}")
        #     if body_dict.get("code") == 200 and body_dict.get("msg") == "OK":
        #         token = body_dict.get("data","").get("access_token")
        #         self.parentWidget.settings.setValue("token",token)
        #         self.parentWidget.api_client.update_token(token) # 更新token
        #     print(f"登录成功: {username}")
        # else:
        #     print(f"登录失败返回: {response.text}")
        self.parentWidget.settings.setValue("username",username)
        self.parentWidget.settings.setValue("password",password)

    def check_login_state(self):
        pass